package cn.ffcs.uec.common.util;

import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.common.dao.UecManagerInfoDao;
import cn.ffcs.uec.common.model.UecManagerInfo;
import cn.ffcs.uec.organization.manager.UecOrgOrgsManager;
import cn.ffcs.uec.outStaff.dao.UecOutStaffDao;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.staff.dao.UecStaffInfoDao;
import cn.ffcs.uec.staff.model.UecStaffInfo;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.PlatformUtil;

public class PermissionUtil {
	private static UecManagerInfoDao uecManagerInfoDao = (UecManagerInfoDao) ApplicationContextUtil
			.getBean("uecManagerInfoDao");

	private static UecStaffInfoDao uecStaffInfoDao = (UecStaffInfoDao) ApplicationContextUtil
			.getBean("uecStaffInfoDao");

	private static UecOutStaffDao uecOutStaffDao = (UecOutStaffDao) ApplicationContextUtil
			.getBean("uecOutStaffDao");

	private static UecOrgOrgsManager uecOrgOrgsManager = (UecOrgOrgsManager) ApplicationContextUtil
			.getBean("uecOrgOrgsManager");
	
	/**
	 * 根据当前用户所属的组织获取上级组织
	 * @return
	 */
	public static String getFullOrg() {
		String code = PlatformUtil.getCurrentUser().getScreenName().toUpperCase();
		if (code.startsWith("W")) {
			UecOutStaffInfo uecOutStaffInfo = uecOutStaffDao
					.findUecOutStaffInfoByPsncode(code);
			if (uecOutStaffInfo != null) {
				return uecOrgOrgsManager.getUecOrgOrgsPreOrgByPkOrg(uecOutStaffInfo.getPkHrorg());
			} else {
				return null;
			}
		} else {
			code=code.substring(1).toUpperCase();
			UecStaffInfo uecStaffInfo = uecStaffInfoDao
					.quaryUecStaffInfoByCode(code);

			if (uecStaffInfo != null) {
				return uecOrgOrgsManager.getUecOrgOrgsPreOrgByPkOrg(uecStaffInfo.getPkOrg());
			} else {
				return null;
			}
		}
	}
	
	/**
	 * 根据当前登录用户，获取所属组织
	 */
	public static String getCurrentOrg() {
		String code = PlatformUtil.getCurrentUser().getScreenName().toUpperCase();
		if (code.startsWith("W")) {
			UecOutStaffInfo uecOutStaffInfo = uecOutStaffDao
					.findUecOutStaffInfoByPsncode(code);
			if (uecOutStaffInfo != null) {
				return uecOutStaffInfo.getPkHrorg();
			} else {
				return null;
			}
		} else {
			code=code.substring(1).toUpperCase();
			UecStaffInfo uecStaffInfo = uecStaffInfoDao
					.quaryUecStaffInfoByCode(code);

			if (uecStaffInfo != null) {
				return uecStaffInfo.getPkOrg();
			} else {
				return null;
			}
		}
	}
	
	/**
	 * 根据当前登录用户，获取所属组织
	 */
	public static String getCurrentOrgName() {
		//String code = PlatformUtil.getCurrentUser().getScreenName()
		//		.substring(1).toUpperCase();
		String code = PlatformUtil.getCurrentUser().getScreenName().toUpperCase();

		if (code.startsWith("W")) {
			UecOutStaffInfo uecOutStaffInfo = uecOutStaffDao
					.findUecOutStaffInfoByPsncode(code);
			if (uecOutStaffInfo != null) {
				return  uecOrgOrgsManager.getUecOrgOrgsNameByPkOrg(uecOutStaffInfo.getPkHrorg());
			} else {
				return null;
			}
		} else {
			code=code.substring(1).toUpperCase();
			UecStaffInfo uecStaffInfo = uecStaffInfoDao
					.quaryUecStaffInfoByCode(code);

			if (uecStaffInfo != null) {
				return uecOrgOrgsManager.getUecOrgOrgsNameByPkOrg(uecStaffInfo.getPkOrg());
			} else {
				return null;
			}
		}
	}
	
	
	
	
	/**
	 * 获取当前登录组织简称
	 * 除了区本部，取组织前两个字母，已达到匹配该组织下的所有子组织
	 * 
	 * @return
	 */
	public static String getManagerOrg() {
     if(PlatformUtil.getCurrentUser()==null) {
    	 ZkUtil.showError("登陆用户已经失效，请重新登录", "温馨提示");
    	 return null;
     }else {
		String code = PlatformUtil.getCurrentUser().getScreenName().toUpperCase();
		if (!code.startsWith("W")) {
			code=code.substring(1).toUpperCase();
		}
		UecManagerInfo queryUecManagerInfo = UecManagerInfo.newInstance();
		queryUecManagerInfo.setCode(code);

		UecManagerInfo uecManagerInfo = uecManagerInfoDao
				.findUecOrgDeptByOrgDept(queryUecManagerInfo);
     
		if (uecManagerInfo != null) {
			return uecManagerInfo.getOrg();
		} else {
			return null;
		}
     }	
	}
	
	/**
	 * 获取当前的用户编码,并判断该编码是不是在权限表uec_manager_info中
	 * 如果不存在就表示该人是外包在职非盟市主管类的人
	 * 如果存在就表示该人是外包盟市主管人员
	 * @return
	 */
	public static boolean isNoMainOutStaff() {
	    String code = PlatformUtil.getCurrentUser().getScreenName().toUpperCase();
		if (!code.startsWith("W")) {
			code=code.substring(1).toUpperCase();
		}
	    UecManagerInfo queryUecManagerInfo = UecManagerInfo.newInstance();
        queryUecManagerInfo.setCode(code);
        UecManagerInfo uecManagerInfo = uecManagerInfoDao
                .findUecOrgDeptByOrgDept(queryUecManagerInfo);
           /**
            *  uecManagerInfo不为空表示该用户是
           	*  盟市主管人员
            */
        if (null != uecManagerInfo) {
            return false;
        } 
	    return true;
	}
	
	
	/**
	 * 根据当前登录用户，获取组织,不一定是一个
	 */
	public static String getOrgRootId(String orgname) {
			return  uecOrgOrgsManager.getOrgRootByName(orgname);
	}
		
}
